package com.tulingxueyuan.mall.modules.pms.controller;


import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.PmsSkuStock;
import com.tulingxueyuan.mall.modules.pms.service.PmsSkuStockService;
import io.netty.util.internal.StringUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sun.awt.SunHints;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * sku的库存 前端控制器
 * </p>
 *
 * @author XuShu
 * @since 2023-05-08
 */
@RestController
@RequestMapping("/sku")
public class PmsSkuStockController {
    @Resource
    private PmsSkuStockService pmsSkuStockService;

    /**
     * 根据商品id查询对应sku
     * @param productId
     * @return
     */
    @GetMapping("/{productId}")
    public CommonResult<List<PmsSkuStock>> listById(
            @PathVariable(value = "productId") Long productId,
            @RequestParam(value = "keyword",defaultValue = "0") String keyword){
        List<PmsSkuStock> list = null;
        if (keyword.equals("0")){
           list = pmsSkuStockService.listByProductId(productId);
            return CommonResult.success(list);
        }else {
            PmsSkuStock pmsSkuStock = pmsSkuStockService.getByCode(productId,keyword);
            list.add(pmsSkuStock);
            return CommonResult.success(list);
        }
    }



    /**
     * 根据商品id查询对应sku
     * @param productId
     * @return
     */
    @PostMapping("update/{productId}")
    public CommonResult update(
            @PathVariable(value = "productId") Long productId,
            @RequestBody List<PmsSkuStock> list){
        boolean b = pmsSkuStockService.update(productId,list);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }


}

